package com.lt.flashcard.view;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Clients;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.AddCoinHistory;
import com.lt.flashcard.model.entity.PaymentResult;
import com.lt.flashcard.model.entity.PhoneCardInfo;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;


/**
 * When user buy new coin
 * @author ocsen
 *
 */
public class AddCoinVm {
	
	private static Logger log = Logger.getLogger(AddCoinVm.class);
	
	private int amount = 20000;
	
	private int cardtype = 0;
	
	private String card_code = "";
	
	private String card_seri = "";

	@Init
	public void init() {
	}
	
	@Command
	public void process() {
		if(!Utility.isLogin()) {
			Utility.loginOrAccessDenied(true);
			Utility.rememberPage("addcoin.zul");
			return;
		}
		
		EcommerceService esrv = Repository.getInstance().getEcommerceService();
		AAService asrv = Repository.getInstance().getAAService();
		AddCoinHistory history;
		switch(payType) {
		case 0: //pay by NganLuong
			history = esrv.newAddCoinHistory(asrv.getCurrentUser(), amount, AddCoinHistory.TYPE_NGANLUONG);
			if(history != null) {
				String url = esrv.generatePaymentUrl(history);
				Utility.rawRedirect(url, true);
			}
			else {
				log.error("Fail to add coin by nganluong");
				Clients.evalJavaScript("showNotice('', 'Kết nối bị lỗi. Vui lòng thử lại hoặc báo với admin.')");
			}
			break;
		case 1: //pay by card
			PhoneCardInfo info = new PhoneCardInfo();
			info.setCard_pin(card_code);
			info.setCard_seri(card_seri);
			if(cardtype == 0)
				info.setType_card(PhoneCardInfo.TYPE_VINAPHONE);
			else if(cardtype == 1)
				info.setType_card(PhoneCardInfo.TYPE_MOBIPHONE);
			else
				info.setType_card(PhoneCardInfo.TYPE_VIETTEL);
			history = esrv.newAddCoinHistory(asrv.getCurrentUser(), amount, AddCoinHistory.TYPE_CARD);
			PaymentResult result = esrv.updatePayment(history, info);
			if(result.isSuccess()) {
				int coin = result.getHistory().getCoin();
				int amount = result.getHistory().getAmount();
				
				Clients.evalJavaScript("showNotice('', '" + String.format("Bạn đã nạp %s đồng và được %s coin", amount, coin) + "')");
			}
			else {
				Clients.evalJavaScript("showNotice('', '" + result.getMessage() + "')");
			}
			break;
		}
	}
	
	@NotifyChange("amount")
	@Command
	public void changeamount(@BindingParam("val") int value) {
		amount = value;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
	
	/**
	 * Payment type
	 */
	private int payType = 0;
	
	@Command
	public void changepaymenttype(@BindingParam("type") int type) {
		payType = type;
	}
	
	@Command
	public void changecardtype(@BindingParam("type") int type) {
		setCardtype(type);
	}

	public String getCard_code() {
		return card_code;
	}

	public void setCard_code(String card_code) {
		this.card_code = card_code;
	}

	public String getCard_seri() {
		return card_seri;
	}

	public void setCard_seri(String card_seri) {
		this.card_seri = card_seri;
	}

	public int getCardtype() {
		return cardtype;
	}

	public void setCardtype(int cardtype) {
		this.cardtype = cardtype;
	}
}
